[#macro fullreductionUpdateView]
<div class="model-form">
    <el-page-header @back="goBack" content="更新满减活动">
    </el-page-header>
    <div class="model-content">
        <el-form ref="ruleForm" :rules="rules" :model="form" label-width="160px">
            <el-row :gutter="10">
                <el-col :span="20">
                    <el-form-item label="名称" size="mini" prop="name">
                        <el-input v-model="form.name" name="name">
                        </el-input>
                    </el-form-item>

                </el-col>
                <el-col :span="20">
                    <el-form-item label="商家" size="mini" prop="shop">
                        <el-select style="width: 100%;" v-model="form.shop" filterable clearable placeholder="请选择">
                            <el-option v-for="item in shopOptions" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                </el-col>
                <el-col :span="20">
                    <el-form-item label="活动时间" size="mini" prop="beginTime">
                        <el-date-picker v-model="sendTime" @change="changeSendTime" type="datetimerange"
                            value-format="yyyy-MM-dd HH:mm" placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="20">
                    <el-form-item label="参与设置" size="mini" prop="fullReductionWay">
                        <el-radio v-model="form.fullReductionWay" label="all">全部商品参与</el-radio>
                        <el-radio v-model="form.fullReductionWay" label="part">限时折扣商品不参与</el-radio>
                    </el-form-item>
                </el-col>
                <el-col :span="20">
                    <el-form-item size="mini" v-for="(item, index) in form.items" :label="'满减项' + index"
                        :key="item.key">
                        满
                        <el-input v-model="form.items[index].fullMoney" style="width: 100px;"></el-input>
                        减
                        <el-input v-model="form.items[index].reduceMoney" style="width: 100px;"></el-input>
                        <el-button @click.prevent="removeItem(index)">删除</el-button>
                        <el-button @click="addItem" type="primary" size="mini">新增属性</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24" style="text-align: right;padding-bottom: 20px;padding-top: 20px;">
                    <el-button @click="goBack">取消</el-button>
                    <el-button type="primary" @click="updateData">确定</el-button>
                </el-col>
            </el-row>
        </el-form>
    </div>
</div>
[/#macro]

[#macro fullreductionUpdateConfig]
<script type="text/x-template" id="my-fullreduction-update">
    [@fullreductionUpdateView /]
</script>
<script type="text/javascript">
    //更新组件开始
    var fullreductionUpdateConfig = {};
    fullreductionUpdateConfig.template = "#my-fullreduction-update";
    fullreductionUpdateConfig.methods = config.basicMethod();
    fullreductionUpdateConfig.methods.updateData = function () {
        var self = this;
        this.$refs["ruleForm"].validate(function (valid) {
            if (valid) {
                self.updateDataPost();
            } else {
                console.log('error submit!!');
                return false;
            }
        });
    }


    fullreductionUpdateConfig.methods.changeSendTime = function (event) {
        console.info(event);
        if (event == null) {
            this.form.sendTime = [];
            this.form.beginTime = "";
            this.form.endTime = "";
        } else {
            this.form.sendTime = event;
            this.form.beginTime = event[0];
            this.form.endTime = event[1];
        }
    }
    fullreductionUpdateConfig.methods.addItem = function () {
        this.form.items.push({
            fullMoney: '',
            reduceMoney: "",
            key: Date.now()
        });
    }
    fullreductionUpdateConfig.methods.removeItem = function (item) {
        if (item > -1) {
            if (this.form.items.length > 1) {
                this.form.items.splice(item, 1)
            } else {
                this.$message.error("满减活动必须保留一项");
            }
        }
    };


    fullreductionUpdateConfig.methods.updateDataPost = function () {
        var self = this, data = this.form;
        //delete data.fullreductionCatalog;
        if (this.deforeUpdateData) {
            this.deforeUpdateData();
        }
        if (this.form.sendTime != null && this.form.sendTime.length == 2) {
            this.form.beginTime = this.form.sendTime[0];
            this.form.endTime = this.form.sendTime[1];
            delete this.form.sendTime;
        }
        this.postJsonData("${siteurl}tenantRest/fullreduction/update.htm", data, function (res) {
            if (res.code == 0) {
                self.$message({
                    message: '修改数据成功',
                    type: 'success'
                });
                //window.history.go(-1)
                self.$router.go(-1);
            } else {
                self.$message.error(res.msg);
            }
        });
    }
    fullreductionUpdateConfig.data = function () {
        return {
            form: {
                name: '',
                shop: '',
                beginTime: '',
                endTime: '',
                fullReductionWay: '',
                sendTime: []
            },
            sendTime: [],
            rules: {
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' }
                ],
                shop: [
                    { required: true, message: '请输入商家', trigger: 'blur' }
                ],
                beginTime: [
                    { required: true, message: '请选择时间', trigger: 'blur' }
                ]

            }
        }
    };

    fullreductionUpdateConfig.mounted = function () {
        var id = this.$route.query.id;
        var self = this;
        var data = {};
        data.id = id;
        this.postData("${siteurl}tenantRest/fullreduction/view.htm", data, function (res) {
            if (res.code == 0) {
                self.form = res;
                if (res.beginTime) {
                    self.form.sendTime = [res.beginTime, res.endTime];
                    self.sendTime = [res.beginTime, res.endTime];
                } else {
                    self.form.sendTime = [];
                    self.sendTime = [];
                }
                if (res.items == null || res.items.length == 0) {
                    self.form.items.push({
                        fullMoney: '',
                        reduceMoney: "",
                        key: Date.now()
                    });
                }
            }
        });
    }
    fullreductionUpdateConfig.computed = {};
    fullreductionUpdateConfig.computed.shopOptions = function () {
        return this.$store.state.shopOptions;
    };

    fullreductionUpdateConfig.destroyed = function () {
        console.log("destroyed add view ");

    }
    var fullreductionUpdateView = Vue.component('update-view', fullreductionUpdateConfig);
    //更新组件结束
</script>
[/#macro]